﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using Frogism.Core;
using System.Data.SqlClient;
using System.Data;

namespace Frogism.Data {
  public class ReadLoader : LoaderUpdateBase<Read, IRead> {
    public Collection<IRead> ByBookId(int bookId) {
      return ProcedureGetAll("ReadGetByBookId", new IntegerParameter() { Name = "BookId", Value = bookId });
    }
    protected override IRead GetEmptyObject() {
      return new Read();
    }
    public void Create(int bookId, IRead obj) {
      using (SqlConnection conn = GetConnection()) {
        SqlCommand cmd = conn.CreateCommand();
        cmd.CommandText = ProcNameCreate;
        cmd.CommandType = CommandType.StoredProcedure;
        AddIntParam(cmd, "BookId", bookId);
        FillParameters(cmd, obj);
        cmd.ExecuteNonQuery();
      }
    }
    public override void Create(IRead read) {
      throw new Exception("Have to use the other Create Command.");
    }
    protected override void FillObject(IRead read,FriendlyReader reader) {
      read.Page = reader.Int("Page");
      read.ReadDate = reader.Date("Date");
    }
    protected override void FillParameters(System.Data.SqlClient.SqlCommand cmd, IRead obj) {
      AddIntParam(cmd, "Page", obj.Page);
      AddDateParam(cmd, "ReadDate", obj.ReadDate);
    }
    protected override string ProcNameGetAll {
        get { return "ReadGetAll"; }
    }
    protected override string ProcNameGetById {
        get { return "ReadGetById"; }
    }
    protected override string ProcNameSave {
      get { return "ReadUpdate"; }
    }

    protected override string ProcNameCreate {
      get { return "ReadInsert"; }
    }

    protected override string ProcNameDelete {
      get { return "ReadDelete"; }
    }
  }
}
